<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>任务列表</title>
</head>
<body>
<section>
    <div>
        <div class="layui-card">
            <div class="adp-content">
                <form id="queryForm"></form>
                <table id="queryTable"></table>
            </div>
        </div>
    </div>
</section>
<div style="width: 600px;display: none;" id="mergeFormDiv">
    <form action="" method="post" id="mergeForm" class="adp-center"></form>
</div>
<div style="width: 600px;display: none;" id="confirmTaskDiv">
    <form action="" method="post" id="confirmTask" class="adp-center"></form>
</div>
</body>
</html>
<script src="../static/adp/import.js"></script>

<script type="text/html" id="OPERATION_ROW_TPL">
    <a class="layui-btn layui-btn-sm" lay-event="edit"> <i class="layui-icon layui-icon-edit"></i>详情/编辑</a>
    <a class="layui-btn layui-btn-sm" lay-event="confirm"> <i class="layui-icon layui-icon-edit"></i>处理异常任务</a>
    <!--a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del"><i
            class="layui-icon layui-icon-delete"></i>删除</a-->
</script>

<script type="text/javascript">
    var queryform = $("#queryForm").adpForm({
        elem: [{type: "text", label: "任务编号", name: "taskId", placeholder: "请输入任务编号"},
            {type: "text", label: "料框编号", name: "containerId", placeholder: "请输入料框遍号"},
            {
                type: "select", label: "任务状态", name: "status", placeholder: "请选择状态",
                data: [{key: "0", value: "所有"}, {key: "等待", value: "等待"}, {key: "完成", value: "完成"}, {
                    key: "异常",
                    value: "异常"
                }]
            },
            {type: "date", label: "按日期查询 从", name: "startDate", placeholder: "请输入关键字"},
            {type: "date", label: "至", name: "endDate", placeholder: "请输入关键字"}],
        button: [{type: "submit", label: "查询", method: "query", icon: 'query'}, {type: "reset", label: "重置"}],
        methods: {
            query: function query(data) {
                queryTable.reload(setStatus());
            }
        }
    });

    /*var mergeForm = $("#mergeForm").adpForm({
        layout: 'vertical',
        elem: [
            {type: "text", label: "详情", name: "details"},
            {type: "hidden", name: "id"}
        ],
        button: [
            {type: "submit", label: "保存", method: "submit"},
            {type: "reset", label: "重置"}
        ],
        methods: {
            submit: function submit(data) {
                layer.confirm('确认将该任务标记为完成?', function (index) {
                    console.log(data);
                    SV("Task")("confirmErrorTask")({id: data.id, details: data.details});
                    queryTable.reload(queryform.data());
                    layer.close(index);
                });
            }
        }

    });*/

    var mergeForm = $("#mergeForm").adpForm({
        layout: 'vertical',
        elem: [
            {type: "text", label: "任务编号", name: "taskId", verify: "required", placeholder: "请输入内容"},
            {type: "text", label: "任务类型", name: "taskType", verify: "required", placeholder: "请输入内容"},
            {type: "text", label: "料框编号", name: "containerId", verify: "required", placeholder: "请输入内容"},
            {type: "text", label: "物料类型", name: "materialType", verify: "required", placeholder: "请输入内容"},
            {type: "text", label: "物料数量", name: "count", verify: "required", placeholder: "请输入内容"},
            {type: "text", label: "物料编号", name: "material", verify: "required", placeholder: "请输入内容"},
            {type: "text", label: "任务状态", name: "status", verify: "required", placeholder: "请输入内容"},
            {type: "text", label: "任务来源", name: "taskFrom", verify: "required", placeholder: "请输入内容"},
            {type: "date", label: "任务日期", name: "date", format: 'yyyy-MM-dd HH:mm:ss', istime: 'true'},
            {type: "date", label: "任务完成日期", name: "completeTime", format: 'yyyy-MM-dd HH:mm:ss', istime: 'true'},
            {type: "hidden", name: "id"}
        ],
        button: [
            {type: "submit", label: "关闭", method: "close"}/*,
            {type: "reset", label: "重置"}*/
        ],
        methods: {
            close: function submit() {
                // SV("Task")("save")(data);
                $("#mergeFormDiv").close();
                queryTable.reload(setStatus());
            }
        }

    });

    var confirmTask = $("#confirmTask").adpForm({
        layout: 'vertical',
        elem: [
            {type: "text", label: "任务编号", name: "taskId", verify: "required", placeholder: "请输入内容"},
            {type: "text", label: "任务类型", name: "taskType", verify: "required", placeholder: "请输入内容"},
            {type: "text", label: "料框编号", name: "containerId", placeholder: "请输入内容"},
            {type: "text", label: "任务状态", name: "status", verify: "required", placeholder: "请输入内容"},
            {type: "text", label: "任务来源", name: "taskFrom", verify: "required", placeholder: "请输入内容"},
            {type: "date", label: "任务日期", name: "date", format: 'yyyy-MM-dd HH:mm:ss', istime: 'true'},
            {type: "text", label: "出库任务料框列表(料框之间请用空格隔开)", name: "containerIds", placeholder: "请输入内容"},
            //containerId,可以添加
            {type: "hidden", name: "id"}
        ],
        button: [
            {type: "submit", label: "提交", method: "submit"}/*,
            {type: "reset", label: "重置"}*/
        ],
        methods: {
            submit: function submit(data) {
                layer.confirm('确认将该任务标记为完成?', function (index) {
                    // console.log(data);
                    var rtv = SV("Task")("confirmErrorTask")({
                        id: data.id,
                        details: data.details,
                        containerIds: data.containerIds
                    });
                    // console.log(rtv);
                    layer.confirm(rtv);
                    $("#confirmTaskDiv").close();
                    queryTable.reload(setStatus());
                    layer.close(index);
                });
            }
        }

    });

    var queryTable = $("#queryTable").adpTable({
        url: '../rs/Task/list',
        where: setStatus(),
        cols: [[
            {field: 'id', title: 'ID', type: 'checkbox', width: 30, sort: true, fixed: 'left'},
            {field: 'taskId', title: '任务编号', width: 200},
            {field: 'taskType', title: '任务类型', width: 130},
            {field: 'containerId', title: '料框编号', width: 140},
            {field: 'materialType', title: '物料类型', width: 80},
            {field: 'material', title: '图号', width: 100},
            {field: 'count', title: '数量', width: 60},
            {field: 'status', title: '状态', width: 70},
            // {field: 'taskFrom', title: '来源', width: 70},
            {field: 'date', title: '任务日期', width: 160},
            {fixed: 'right', title: '操作', width: 280, align: 'center', toolbar: '#OPERATION_ROW_TPL'}
        ]],
        methods: {
            edit: function edit(data, tr) {
                $("#mergeFormDiv").open("编辑任务");
                mergeForm.reset();
                mergeForm.data(data);
            },
            del: function del(data, tr) {
                layer.confirm('真的删除行么', function (index) {
                    SV("Task")("delete")({ids: [data.id]});
                    queryTable.reload(setStatus());
                    layer.close(index);
                });
            },
            confirm: function confirm(data, tr) {
                $("#confirmTaskDiv").open("处理任务");
                confirmTask.reset();
                confirmTask.data(data);
            },
        }
    });

    function addEntity() {
        return $("#mergeFormDiv").open("添加任务", {
            success: function success() {
                return mergeForm.reset();
            }
        });
    };

    function deleteAllEntity(arr) {
        var _queryTable$checkStat = queryTable.checkStatus(),
            data = _queryTable$checkStat.data;
        var ids = new Array();
        $.each(data, function (n, value) {
            ids.push(value.id);
        });
        layer.confirm('真的删除行么', function (index) {
            Deferred("Task")("delete")({
                "ids": ids
            }).then(function () {
                queryTable.reload(setStatus());
                layer.close(index);
            });
        });
    };

    function setStatus() {
        var para = queryform.data();
        if (0 == para.status) {
            para.status = null;
        }
        return para;
    }
</script>